'use client';


import { Knowledge, KnowledgePoint } from '@/data/prisma';
import EditableTable, { IExpandable, IRefData } from '@/pao/web/dataEdit/dateEditEditableTable';
import { ISelectableControlProps, SelectableTable } from '@/pao/web/dataEdit/dataEditSelectableTable';
import { ProCard, ProColumnType, ProForm, ProFormGroup, ProFormList, ProFormSelect, ProFormText } from '@ant-design/pro-components';

import { addRow, deleteRow, editRow, query } from './service';
import { query as queryGrade } from '@/data/modules/grade/service';
import { query as querySubject } from '@/data/modules/subject/service';

const columns: ProColumnType<Knowledge>[] = [
    {
        title: '编号',
        dataIndex: 'code',
        fixed: 'left',
        sorter: true,
        width: '150px',
    },
    {
        title: '名称',
        dataIndex: 'name',
        sorter: true,
        fixed: 'left',
        width: '150px',
    }, 
		{
				title: '年级',
				dataIndex: 'gradeId',
				sorter: true,
				valueType: 'select',
				width: '200px',
				request: async (row) => {
						const grades = await queryGrade({id: row.gradeId }, {});
						return grades.rows.map(grade => ({
								label: `${grade.code} - ${grade.name}`,
								value: grade.id,
						}));
				}
		},
		{
				title: '科目',
				dataIndex: 'subjectId',
				sorter: true,
				valueType: 'select',
				width: '200px',
				request: async (row) => {
						const subjects = await querySubject({id: row.subjectId }, {});
						return subjects.rows.map(subject => ({
								label: `${subject.code} - ${subject.name}`,
								value: subject.id,
						}));
				}
		},
    {
        title: '备注',
        dataIndex: 'comments',
        sorter: true,
        width: 'auto',
    },
];

const config = {
    headerTitle: '知识',
    tooltip: '修改知识信息',
}

export default function KnowledgeControl({refData, expandTable, getRefData}: IRefData<Knowledge> & IExpandable<Knowledge, KnowledgePoint>) {
    return <EditableTable<Knowledge, KnowledgePoint> columns={columns} rowKey="id" config={config} deleteRow={deleteRow} query={query} editRow={editRow} addRow={addRow} refData={refData} expandTable={expandTable} getRefData={getRefData}/>
}

const selectColumns: ProColumnType<Knowledge>[] = [
	{
			title: '编号',
			dataIndex: 'code',
			fixed: 'left',
			sorter: true,
			width: '40px',
	},
	{
			title: '名称',
			dataIndex: 'name',
			sorter: true,
			fixed: 'left',
			width: '80px',
	}
];

export function KnowledgeSelectControl({onSelectRow}: ISelectableControlProps<Knowledge>) {
	return <SelectableTable onSelectRow={onSelectRow} columns={selectColumns} rowKey="id" query={query}/>
}